<template>
  <div id="Recharge">
    <com-head :opacity="0">书币充值</com-head>
    <p>书币充值&nbsp;1元=1书币</p>
    <div class="recharge">
      <div class="item" @click="rechargeMoney(item)" v-for="(item,index) in charge" :key="index">
        <p>
          {{item}}
          <span>元</span>
        </p>
        <p>{{item}}书币</p>
      </div>
    </div>
    <div class="warm">
      <p>温馨提示</p>
      <p>1、通过微信充值书币，可以在“个人中心”-“充值消费记录”中查看充值记录。</p>
      <p>2、在提现过程中可能会有延迟到账的情况，如果长时间未到账，请从“个人中心”-“客服反馈”里进行反馈，客服人员会尽快进行处理。</p>
      <!-- <p>3、点击充值，代表您已阅读并同意
        <router-link :to="{name: 'protocol',query: {status: '1'}}" tag="a">《用户协议》</router-link>和
        <router-link :to="{name: 'protocol',query: {status: '2'}}" tag="a">《隐私协议》</router-link>。
      </p> -->
    </div>
  </div>
</template>

<script>
export default {
  name: "Recharge",
  data() {
    return {
      isDisable: '',
      charge: []
    };
  },

  computed: {},

  created() {},

  mounted() {
    this.loading();
  },

  methods: {
    rechargeMoney(num) {
      if(!this.isDisable){
        this.isDisable = true; 
        setTimeout(() => {
        this.isDisable = false;
        }, 1000) 
        this.axios
        .post("user/topup", {
          token: this.token(),
          price: num
        })
        .then(({ data }) => {
          console.log(data);
          if (data.code === "200") {
            const jsApiParameters = data.data;
            this.jsSdk(jsApiParameters);
          } else if (data.code === "201") {
            this.$bus.$emit("toast", data.msg);
          }
        })
        .catch(function(error) {
          console.log(error);
        });      
      }
    },
    loading() {
      this.axios
        .post("user/money")
        .then(({ data }) => {
          console.log(data);
          if (data.code === "200") {
            this.charge = data.data;
          } else if (data.code === "201") {
            this.$bus.$emit("toast", data.msg);
          }
        })
        .catch(function(error) {
          console.log(error);
        });
    },
    jsSdk(jsApiParameters) {
      WeixinJSBridge.invoke(
        "getBrandWCPayRequest",
        {
          appId: jsApiParameters.appId,
          package: jsApiParameters.package,
          nonceStr: jsApiParameters.nonceStr,
          timeStamp: jsApiParameters.timeStamp,
          signType: jsApiParameters.signType,
          paySign: jsApiParameters.paySign
        },
        // jsApiParameters,
        function(res) {
          WeixinJSBridge.log(res.err_msg);
          var result = res.err_msg;
          // alert(JSON.stringify(res));
          // return;
          if (result == "get_brand_wcpay_request:ok") {
            alert("支付成功");
            var url = "http://shopq.xiaohanbo.com/userCenter";
          } else {
            alert("你取消了支付");
            var url = "http://shopq.xiaohanbo.com/Recharge";
          }
          window.location.href = url;
        }
      );
    }
  }
};
</script>
<style lang='scss' scoped>
#Recharge {
  p {
    padding: 0 32px;
    line-height: 76px;
    color: rgba(85, 85, 85, 1);
    font-size: 26px;
  }
  .recharge {
    margin: 0 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    .item {
      width: 342px;
      height: 200px;
      background-color: #fff;
      margin-left: 2px;
      margin-bottom: 2px;
      text-align: center;
      p:nth-of-type(1) {
        margin-top: 50px;
        font-size: 40px;
        color: #ec0202;
        line-height: 50px;
        span {
          font-size: 26px;
        }
      }
      p:nth-of-type(2) {
        font-size: 24px;
        color: #555;
        line-height: 40px;
      }
    }
  }
  .warm {
    margin: 100px 0px 0;
    font-size: 28px;
    color: rgba(85, 85, 85, 1);
    text-align: left;
  }
}
</style>